﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Profile;

using MP.LayerHelper.MP_OBS.BLL; // using lay co .cs xu ly
using MP.LayerHelper.MP_OBS.DAL.EntityClasses; // using cac doi tuong entity
using MP.LayerHelper.MP_OBS.DAL.HelperClasses; // using cac ham ho tro doi tuong entity

using MP.Core.Library;
using MP.Core.Config;

public partial class Member_Control_UpdateProfile : MP.Core.Authentication.Control.AuthenBase
{
    public string userName
    {
        
        //get { return Request["user"]; }
        get { return string.IsNullOrEmpty(Request["user"]) ? Profile.WindowsUser : Request["user"]; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadProject();
            string st1 = WebUtility.GetLockButtonJscript(this.Page, btnUpdate, "Đang cập nhật dữ liệu ...", "");
            btnUpdate.OnClientClick = st1;
        }
    }

    void LoadProject()
    {
        if (!string.IsNullOrEmpty(userName) && Membership.FindUsersByName(userName).Count > 0)
        {
            HrEmployeeEntity obj = HrEmployeeManager.CreateInstant().GetByWindowUser(userName);
            if(obj!=null)
            {
                txtID.Text = obj.EmployeeId;
                txtFullName.Text = obj.FullName;
                txtCode.Text = obj.MachineId;
                txtEmail.Text = obj.Email;
                txtMobile.Text = obj.MobilePhone;
            }
        }
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (Page.IsValid && IsEdit())
        {
            HrEmployeeEntity obj = HrEmployeeManager.CreateInstant().GetByWindowUser(userName);
            if (obj != null)
            {
                obj.EmployeeId = txtID.Text.Trim();
                obj.FullName = txtFullName.Text.Trim();
                obj.MachineId = txtCode.Text.Trim();
                obj.Email = txtEmail.Text.Trim();
                obj.MobilePhone = txtMobile.Text.Trim();
                obj.UpdatedBy = Util.CurrentUserName;
                obj.UpdatedDate = DateTime.Now;
                HrEmployeeManager.CreateInstant().Update(obj);
            }
        }
        else if(!IsEdit())
        {
            RedirectLogin();
        }
    }
    protected void btCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Portal/");
    }
}
